package org.dxy.infrastructure.persistent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dxy.infrastructure.persistent.po.query.MaterialGroupViewPo;
import org.dxy.infrastructure.persistent.mapper.MaterialGroupViewMapper;
import org.dxy.infrastructure.persistent.service.MaterialGroupViewService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物料群组视图服务实现
 * @author dxy
 * @createTime 2025/9/5
 */
@Service
public class MaterialGroupViewServiceImpl extends ServiceImpl<MaterialGroupViewMapper, MaterialGroupViewPo> implements MaterialGroupViewService {

    @Override
    public boolean existsByGroupCode(String groupCode) {
        if (groupCode == null || groupCode.trim().isEmpty()) {
            return false;
        }
        return lambdaQuery()
                .eq(MaterialGroupViewPo::getGroupCode, groupCode)
                .exists();
    }

    @Override
    public List<MaterialGroupViewPo> getByParentGroupId(Long parentGroupId) {
//        if (parentGroupId == null) {
//            return List.of();
//        }
//        return list(new LambdaQueryWrapper<MaterialGroupViewEntity>()
//                .eq(MaterialGroupViewEntity::getParentGroupId, parentGroupId)
//                .orderByAsc(MaterialGroupViewEntity::getSortOrder)
//                .orderByAsc(MaterialGroupViewEntity::getCreatedTime));
        return null;
    }

    @Override
    public List<MaterialGroupViewPo> getRootGroups() {
//        return list(new LambdaQueryWrapper<MaterialGroupViewEntity>()
//                .isNull(MaterialGroupViewEntity::getParentGroupId)
//                .orderByAsc(MaterialGroupViewEntity::getSortOrder)
//                .orderByAsc(MaterialGroupViewEntity::getCreatedTime));
        return null;

    }

    @Override
    public List<MaterialGroupViewPo> getByGroupType(String groupType) {
        if (groupType == null || groupType.trim().isEmpty()) {
            return List.of();
        }
        return list(new LambdaQueryWrapper<MaterialGroupViewPo>()
                .eq(MaterialGroupViewPo::getGroupType, groupType)
                .orderByAsc(MaterialGroupViewPo::getId));
//                .orderByAsc(MaterialGroupViewEntity::getCreatedTime));
    }

    @Override
    public List<MaterialGroupViewPo> getActiveGroups() {
        return list(new LambdaQueryWrapper<MaterialGroupViewPo>()
                .eq(MaterialGroupViewPo::getStatus, "ACTIVE")
                .orderByAsc(MaterialGroupViewPo::getId));
//                .orderByAsc(MaterialGroupViewEntity::getCreatedTime));
    }

    @Override
    public long countByParentGroupId(Long parentGroupId) {
//        if (parentGroupId == null) {
//            return 0;
//        }
//        return count(new LambdaQueryWrapper<MaterialGroupViewEntity>()
//                .eq(MaterialGroupViewEntity::getParentGroupId, parentGroupId));
        return 0;

    }
}